package com.imooc.coupon.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求日志过滤器
 * Created By TINY
 * Create Time 2020/2/9 - 12:03
 **/
@Slf4j
@Component
public class AccessLogFilter extends AbstractPostZuulFilter {
    @Override
    protected Object cRun() {
        HttpServletRequest request = context.getRequest();

        // 获取请求开始时间
        long startTime = (long) context.get("startTime");

        // 请求uri
        String requestURI = request.getRequestURI();

        long duration = System.currentTimeMillis() - startTime;

        log.info("uri: {}, duration: {}", requestURI, duration);

        return success();
    }

    @Override
    public int filterOrder() {
        // 要想让自定义的post过滤器生效，（优先级）必须小于SendResponseFilter（1000）
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1;
    }
}
